﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AppLab.Data.Models;

namespace AppLab.Web
{
    /// <summary>
    /// download AnythingElse
    /// </summary>
    public class GetAnythingElse : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            var guid = context.Request.QueryString["guid"];
            if (guid != null)
            {
                var id = new Guid(guid);
                byte[] content = null;
                string _fileName = "default.c";
                using (var dc = PMSEntities.Create())
                {
                    tysx_AppFlow reflow = dc.tysx_AppFlow.First(p => p.FlowID == id);
                    content = reflow.tysx_Submit.AnythingElse;
                    _fileName = reflow.tysx_Submit.AnythingElseName;
                }

                context.Response.AddHeader("Connection", "Keep-Alive");
                context.Response.ContentType = "application/octet-stream";
                context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8).Replace("+","%20")));
                context.Response.BinaryWrite(content);
            }
            else
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("Guid不能为空");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}